package com.wwu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwu.common.constant.CommConst;
import com.wwu.common.utils.AssertUtil;
import com.wwu.common.web.ResultObj;
import com.wwu.system.entity.SysUser;
import com.wwu.system.entity.SysUserRole;
import com.wwu.system.mapper.SysUserRoleMapper;
import com.wwu.system.service.ISysUserRoleService;
import com.wwu.system.vo.XmSelectVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *  系统用户角色服务实现类
 * @author 一蓑烟雨
 * @date 2021-10-26
 * @version 1.0.0
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 根据用户id查询用户的角色树
     * @param userId
     * @return com.wwu.common.web.ResultObj
     * @author 一蓑烟雨
     * @date 2023/6/18 14:13
     */
    @Override
    public ResultObj userRoleTree(String userId) {
        List<XmSelectVO> roleXmSelect = sysUserRoleMapper.userRoleTree(userId);
        return ResultObj.success("用户角色树查询成功",roleXmSelect);
    }

    /**
     * 更新用户角色
     * @param sysUser 用户角色
     * @author 一蓑烟雨
     * @date 2023/6/19 17:50
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateUserRoleByUserId(SysUser sysUser) {
        AssertUtil.isTrue(!StringUtils.hasText(sysUser.getUserId()),"用户Id不能为空");
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId,sysUser.getUserId());

       //如果数量大于0则执行删除操作
       if(this.count(wrapper) > 0){
           AssertUtil.isTrue(!this.remove(wrapper),"用户角色删除失败");
       }
       //遍历用户角色
        List<SysUserRole> sysUserRoles = new ArrayList<>();
        for (String roleId: sysUser.getRoleIds().split(CommConst.SPLIT_COMMA)) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(sysUser.getUserId());
            sysUserRole.setRoleId(roleId);
            sysUserRoles.add(sysUserRole);
        }
         AssertUtil.isTrue(!this.saveBatch(sysUserRoles),"用户角色更新失败");;
    }

    /**
     * 根据用户id删除用户角色
     * @param userIds  用户id集合
     * @author 一蓑烟雨
     * @date 2023/6/19 17:50
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteUserRoleByUserId(List<String> userIds) {
        AssertUtil.isTrue(ObjectUtils.isEmpty(userIds),"用户Id不能为空");
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysUserRole::getUserId,userIds);

        //如果数量大于0则执行删除操作
        if(this.count(wrapper) > 0){
            AssertUtil.isTrue(!this.remove(wrapper),"用户角色删除失败");
        }
    }

    /**
     * 根据角色id删除用户角色
     * @param roleIds  角色id集合
     * @author 一蓑烟雨
     * @date 2023/6/20 9:03
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public boolean deleteUserRoleByRoleId(List<String> roleIds) {
        AssertUtil.isTrue(ObjectUtils.isEmpty(roleIds),"角色Id不能为空");
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysUserRole::getRoleId,roleIds);

        //如果数量大于0则执行删除操作
        if(this.count(wrapper) > 0){
            AssertUtil.isTrue(!this.remove(wrapper),"用户角色删除失败");
        }
        return true;
    }
}
